package com.zhhh.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.zhhh.authorization.annotation.Authorization;
import com.zhhh.config.ResultStatus;
import com.zhhh.domain.FilePath;
import com.zhhh.domain.User;
import com.zhhh.model.ResultModel;
import com.zhhh.repository.FilePathRepository;
import com.zhhh.service.FilePathService;

@Controller
@RequestMapping("/file")
public class FileController {
	// 访问路径为：http://127.0.0.1:8080/file
//    @RequestMapping("/file")
//    public String file(){
//        return "/file";
//    }
//    
//    @RequestMapping("/mutifile")
//    public String mutifile(){
//        return "/mutifile";
//    }
	@Autowired
	private FilePathRepository filePathRepository;

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ApiOperation(value = "单文件上传")
	@ResponseBody
	public String handleFileUpload(@RequestParam("file") MultipartFile file) {
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}

			return "上传成功";

		} else {

			return "上传失败，因为文件是空的.";

		}
	}

	/**
	 * 
	 * 多文件具体上传时间，主要是使用了MultipartHttpServletRequest和MultipartFile
	 * 
	 * @param request
	 * 
	 * @return
	 * 
	 */

	@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
	@ApiOperation(value = "多文件上传")
	public @ResponseBody String handleFileUpload(HttpServletRequest request) {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		MultipartFile file = null;
		BufferedOutputStream stream = null;
		for (int i = 0; i < files.size(); ++i) {
			file = files.get(i);
			if (!file.isEmpty()) {
				try {
					byte[] bytes = file.getBytes();
					stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
					stream.write(bytes);
					stream.close();
				} catch (Exception e) {
					stream = null;
					return "You failed to upload " + i + " =>" + e.getMessage();
				}
			} else {
				return "You failed to upload " + i + " becausethe file was empty.";
			}
		}
		return "upload successful";

	}

	@Autowired
	private FilePathService filePathService;

	// 传入的参数file是我们指定的文件
	@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
	@ApiOperation(value = "上传文档文件转换PDF")
	@ResponseBody
	public ResponseEntity<ResultModel> upload(@RequestParam("file") MultipartFile file) {
		List<FilePath> findAll;
		if (filePathService.Upload(file)) {
			findAll = (List<FilePath>) filePathRepository.findAll();
			return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
		} else {
			findAll = (List<FilePath>) filePathRepository.findAll();
			return new ResponseEntity<>(ResultModel.error(ResultStatus.FILE_CONVERSION_ERROR, findAll), HttpStatus.OK);
		}

	}

	@RequestMapping(value = "/queryFileAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有文件")
	public ResponseEntity<ResultModel> queryUserAll() {

		List<FilePath> findAll = (List<FilePath>) filePathRepository.findAll();

		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

}
